<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>demo</title>
		<style type="text/css">
			#box{width: 100px; height: 100px;background: red;color: white;position: absolute;}
		</style>
	</head>
	<body>
		<div id="box">这是一个盒子</div>
	</body>
	<script type="text/javascript">
		
		fn('box');
		function fn(obj){
			var box=document.getElementById(obj);
			var disX=0;
			var disY=0;
			box.onmousedown=function(ev){
				var ev=ev ||window.event;
				disX=ev.clientX-box.offsetLeft;
				disY=ev.clientY-box.offsetTop;
				document.onmousemove=function(ev){
					var ev =ev||window.event;
					box.style.left=ev.clientX-disX+'px';
					box.style.top=ev.clientY-disY+'px';
				}
				document.onmouseup=function(){
					document.onmousemove=null;document.onmouseup=null;
				}
				return false;
			}
			
		}
	
	</script>
</html>
